﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Enring.Data;
using System.Data;

namespace Enring
{
    /// <summary>
    /// This class is used to create a AppUI.xml automaticly
    /// </summary>
    public class AppUICreator
    {
        private XmlDocument doc;
        private DataMapping dm;
        public bool ShowDetailTableInTabs { get; set; }
        public bool ShowDefaultActions { get; set; }
        public bool UseSqlAdapter { get; set; }
        public bool ShowCheckBox { get; set; }
       
        public AppUICreator()
        {
            doc = new XmlDocument();
            dm = new DataMapping(null);
        }

        public XmlNode CreateAppliaction(string schema,string title)
        {
            return null;
        }

        public XmlNode CreateForm(string masterTable,string detailTables)
        {
            XmlNode formNode = doc.CreateElement("Form");

            return null;
        }

        public XmlNode CreateDataNode(string table)
        {
            return null;
        }

        public XmlNode CreateFieldSetNode(string table)
        {
            XmlNode fieldSetNode = doc.CreateElement("FieldSet");
            DataSet dsFields = dm.GetColumns(table);
            if (DB.HasData(dsFields))
            {
                foreach (DataRow col in dsFields.Tables[0].Rows)
                {
                    fieldSetNode.AppendChild(CreateFieldNode(col));
                }
            }
            return fieldSetNode;
        }

        protected XmlNode CreateFieldNode(DataRow row)
        {
            XmlNode fieldNode = doc.CreateElement("Field");
            string rowDataType;
            string field;
            string title;
            bool isnull;
            int length;
            GetPropertiesFromRow(row, out rowDataType, out field, out title, out isnull, out length);

            dm.AddAttributeToNode(doc, fieldNode, "title", title);
            dm.AddAttributeToNode(doc, fieldNode, "must_fill", isnull?"false":"true");
            dm.AddAttributeToNode(doc, fieldNode, "field", field);
            
            XmlNode uiNode = doc.CreateElement("UI");
            string uiType = "SingleText";
            string lines = "3";
            string uiClass = "input-text-medium";
            if (rowDataType.EndsWith("char") || rowDataType.EndsWith("xml"))
            {
                if (length > 100)
                {
                    uiType = "MultiText";
                    dm.AddAttributeToNode(doc, uiNode, "lines", lines);
                }
                else if(length > 50)
                {
                    uiClass = "input-text-large";
                }
                else if (length < 10)
                {
                    uiClass = "input-text-short";
                }
            }
            else if (rowDataType.EndsWith("int") || rowDataType.EndsWith("float") || rowDataType.EndsWith("double")
                 || rowDataType.EndsWith("real") || rowDataType.EndsWith("money") || rowDataType.EndsWith("bit"))
            {
                uiType = "SingleText";
            }
            else if (rowDataType.EndsWith("datetime") || rowDataType.EndsWith("datetime2"))
            {
                uiType = "DateTime";
            }
            dm.AddAttributeToNode(doc, uiNode, "type", uiType);
            //dm.AddAttributeToNode(doc, uiNode, "size", "");
            dm.AddAttributeToNode(doc, uiNode, "class", uiClass);

            fieldNode.AppendChild(uiNode);
            return fieldNode;
        }

        private static void GetPropertiesFromRow(DataRow row, out string rowDataType, out string field, out string title, out bool isnull, out int length)
        {
            rowDataType = row["DATA_TYPE"].ToString();
            field = row["COLUMN_NAME"].ToString();
            title = row["DESCRIPTION"].ToString();
            isnull = Convert.ToBoolean(row["ISNULLABLE"]);
            length = Convert.ToInt32(row["LENGTH"]);
        }

        public XmlNode CreateViewNode(string table)
        {
            XmlNode viewNode = doc.CreateElement("FieldSet");

            dm.AddAttributeToNode(doc, viewNode, "guid", Guid.NewGuid().ToString("B"));
            dm.AddAttributeToNode(doc, viewNode, "name", table);
            dm.AddAttributeToNode(doc, viewNode, "show_title", "true");

            viewNode.AppendChild(CreateDataNode(table));
            viewNode.AppendChild(CreateViewToolsNode("", "", ""));

            XmlNode gridViewNode = doc.CreateElement("GridView");

            DataSet dsFields = dm.GetColumns(table);
            if (DB.HasData(dsFields))
            {
                if (ShowCheckBox)
                {
                    XmlNode checkBoxNode = doc.CreateElement("Column");
                    dm.AddAttributeToNode(doc, checkBoxNode, "type", "checkbox");
                    dm.AddAttributeToNode(doc, checkBoxNode, "class", "center");
                    dm.AddAttributeToNode(doc, checkBoxNode, "width", "5%");
                    gridViewNode.AppendChild(checkBoxNode);
                }
                foreach (DataRow col in dsFields.Tables[0].Rows)
                {
                    gridViewNode.AppendChild(CreateViewColumnNode(col));
                }
            }

            viewNode.AppendChild(gridViewNode);

            return viewNode;
        }

        /// <summary>
        /// Create sample view tools node
        /// </summary>
        /// <returns></returns>
        protected XmlNode CreateViewToolsNode(string viewid,string dialogFormId,string dialogTitle)
        {
            XmlNode toolsNode = doc.CreateElement("Tools");
            XmlNode addToolNode = doc.CreateElement("Tool");
            XmlNode delToolNode = doc.CreateElement("Tool");

            dm.AddAttributeToNode(doc, addToolNode, "class", "action-add");
            dm.AddAttributeToNode(doc, addToolNode, "title", "New Item");
            dm.AddAttributeToNode(doc, addToolNode, "target", "self");
            dm.AddAttributeToNode(doc, addToolNode, "href", string.Format("{dialogopen:{0}:pid=request[dataid]}",dialogFormId));
            dm.AddAttributeToNode(doc, addToolNode, "aclass", "cm-dialog-opener text-button-edit cm-ajax-update");
            dm.AddAttributeToNode(doc, addToolNode, "id", string.Format("opener_add_new_{0}",viewid));
            dm.AddAttributeToNode(doc, addToolNode, "rev", string.Format("content_add_new_{0}",viewid));
            dm.AddAttributeToNode(doc, addToolNode, "dialog_title", dialogTitle);

            dm.AddAttributeToNode(doc, delToolNode, "class", "action-btn");
            dm.AddAttributeToNode(doc, delToolNode, "title", "Delete Selected");
            dm.AddAttributeToNode(doc, delToolNode, "aclass", "cm-confirm text-button-edit cm-ajax-update");
            
            toolsNode.AppendChild(addToolNode);
            toolsNode.AppendChild(delToolNode);

            return toolsNode;
        }

        protected XmlNode CreateViewColumnNode(DataRow row)
        {
            XmlNode colNode = doc.CreateElement("Column");
            string rowDataType;
            string field;
            string title;
            bool isnull;
            int length;
            GetPropertiesFromRow(row, out rowDataType, out field, out title, out isnull, out length);

            

            return colNode;
        }
    }
}
